package com.tms.app.tms.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.tms.app.tms.R;
import com.tms.app.tms.service.AlarmService;
import com.tms.app.tms.test.utils.ServiceUtil;
import com.tms.app.tms.ui.base.ui.BaseListActivity;
import com.tms.app.tms.ui.home.history.TaskFt;
import com.tms.app.tms.ui.home.info.HomeFt;
import com.tms.app.tms.ui.home.market.MarketFt;
import com.tms.app.tms.ui.home.me.AboutMeFt;
import com.tms.app.tms.ui.home.msg.OrderFt;
import com.tms.app.tms.utils.LocationUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
@Deprecated
public class HomeActivity extends BaseListActivity
        implements NavigationView.OnNavigationItemSelectedListener, RadioGroup.OnCheckedChangeListener, AMapLocationListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.nav_info)
    RadioButton navInfo;
    @BindView(R.id.nav_trade)
    RadioButton navTrade;
    @BindView(R.id.nav_history)
    RadioButton navHistory;
    @BindView(R.id.nav_c2c)
    RadioButton navC2c;
    @BindView(R.id.nav_menu)
    RadioGroup navMenu;
    @BindView(R.id.frame)
    FrameLayout frame;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private HomeFt homeFt;// 任务
    private OrderFt orderFt;//消息
    private MarketFt marketFt;
    private TaskFt taskFt;//历史任务
    private AboutMeFt aboutFt;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        context=this;
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navMenu.setOnCheckedChangeListener(this);
        showFragment(0);
        mapLocationClient=LocationUtil.getLocationClient(this);
        mapLocationClient.setLocationOption(LocationUtil.initDefaultOption());
        mapLocationClient.setLocationListener(this);
        mapLocationClient.startLocation();

         startService(new Intent(this, AlarmService.class));

        ServiceUtil.startServiceIfDead(this,AlarmService.class);
    }


    public void showFragment(int checkedId) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        switch (checkedId) {
            case R.id.nav_info:
            case 0:
                if (homeFt == null) {
                    homeFt = new HomeFt();
                    ft.add(R.id.frame, homeFt);
                } else {
                    ft.show(homeFt);
                }
                break;
            case R.id.nav_trade:
            case 1:
                if (orderFt == null) {
                    orderFt = new OrderFt();
                    ft.add(R.id.frame, orderFt);
                } else {
                    ft.show(orderFt);
                }
                break;
            case R.id.nav_market:
            case 2:
                if (marketFt == null) {
                    marketFt = new MarketFt();
                    ft.add(R.id.frame, marketFt);
                } else {
                    ft.show(marketFt);
                }
                break;
            case R.id.nav_history:
            case 3:
                if (taskFt == null) {
                    taskFt = new TaskFt();
                    ft.add(R.id.frame, taskFt);
                } else {
                    ft.show(taskFt);
                }

                break;
            case R.id.nav_c2c:
            case 4:
                if (aboutFt == null) {
                    aboutFt = new AboutMeFt();
                    ft.add(R.id.frame, aboutFt);
                } else {
                    ft.show(aboutFt);
                }
                break;
        }
        ft.commitNow();
    }

    public void hideFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (homeFt != null)
            ft.hide(homeFt);
        if (orderFt != null)
            ft.hide(orderFt);
        if (marketFt!=null)
            ft.hide(marketFt);
        if (taskFt != null)
            ft.hide(taskFt);
        if (aboutFt != null)
            ft.hide(aboutFt);
        ft.commitNow();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        hideFragment();
        showFragment(checkedId);
    }

    @Override
    public void onLocationChanged(AMapLocation location) {
        if(location.getErrorCode() == 0){
            Toast.makeText(context,"定位成功",1000).show();
            mapLocationClient.stopLocation();
        }
    }
}
